package bo;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;

import jxl.read.biff.BiffException;
import vo.User;

/**
 * Servlet implementation class Import
 */
@WebServlet("/Import")
public class Import extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Import() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String save=this.getServletContext().getRealPath("");//文件要保存的路径
		File saveer=new File(save);
		if(!saveer.exists()){
			saveer.mkdirs();
		}
		int max_size=5*1024*1024;
		MultipartRequest multi;
		multi=new MultipartRequest(request,save,max_size,"utf-8");
		Enumeration<String>files=multi.getFileNames();
		String name=files.nextElement();
		System.out.println(name);
		File f=multi.getFile(name);
		try {
			book.ImportData(f);	
		request.getRequestDispatcher("importsuccess.jsp").forward(request,response);
		} catch (BiffException | ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
